package xyz.mumuwei.youthcourseserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import xyz.mumuwei.youthcourseserver.common.dto.PageDto;
import xyz.mumuwei.youthcourseserver.common.lang.Result;
import xyz.mumuwei.youthcourseserver.pojo.Role;
import xyz.mumuwei.youthcourseserver.service.RoleService;

import java.util.LinkedList;
import java.util.List;

/**
 * @author mumuwei
 * @since 2022-02-19
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 获取角色列表-分页
     * @param pageDto 分页参数
     */
    @GetMapping("/page")
    public Result page(PageDto pageDto){
        Page<Role> rolePage = new Page<>(pageDto.getCurrent(), pageDto.getSize());
        IPage<Role> roleList = roleService.page(rolePage, new QueryWrapper<Role>()
                .like("code", pageDto.getKey())
                .or()
                .like("name", pageDto.getKey())
                .orderByDesc("create_time"));
        if(roleList == null){
            return Result.fail("获取角色列表！");
        }
        // 权限id字符串转数组存储
        List<Role> records = roleList.getRecords();
        strToList(records);
        return Result.success(roleList);
    }

    /**
     * 获取角色列表-不分页
     */
    @GetMapping("/list")
    public Result list(){
        List<Role> roleList = roleService.list(new QueryWrapper<Role>()
                .orderByDesc("create_time"));
        if(roleList == null){
            return Result.fail("获取角色列表！");
        }
        // 权限id字符串转数组存储
        strToList(roleList);
        return Result.success(roleList);
    }

    /**
     * 根据id获取角色信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Role role = roleService.getById(id);
        if(role == null){
            return Result.fail("角色不存在！");
        }
        authIdStrToList(role);
        return Result.success(role);
    }

    /**
     * 创建角色
     */
    @PostMapping("")
    public Result create(@Validated Role role){
        boolean save = roleService.save(role);
        if(save){
            return Result.success(role);
        }
        return Result.fail("创建失败！请重试。");
    }

    /**
     * 更新角色信息
     */
    @PostMapping("/update")
    public Result update(@Validated Role role){
        boolean b = roleService.updateById(role);
        if(b){
            return Result.success("更新成功！");
        }
        return Result.fail("更新失败！");
    }

    /**
     * 根据id删除角色
     */
    @GetMapping("/d_{id}")
    public Result delete(@PathVariable Integer id){
        boolean b = roleService.removeById(id);
        if(b){
            return Result.success("删除成功！");
        }
        return Result.fail("删除失败");
    }

    /**
     * 角色权限分配
     */
    @PostMapping("/authority")
    public Result authority(Role role){
        boolean update = roleService.updateById(role);
        if(update){
            return Result.success("权限分配成功！");
        }
        return Result.fail("权限分配失败！");
    }

    /**
     * 角色list中 权限id字符串 转 数组
     * @param roleList 角色列表
     */
    private void strToList(List<Role> roleList){
        for(Role role : roleList){
            authIdStrToList(role);
        }
    }

    /**
     * 单一角色 字符串转数组
     */
    private void authIdStrToList(Role role){
        String authStr = role.getAuthorityId();
        if(authStr != null && authStr.length() != 0){
            String[] auth = authStr.split(",");
            List<Integer> authIds = new LinkedList<>();
            for (String a : auth){
                authIds.add(Integer.parseInt(a));
            }
            role.setAuthIds(authIds);
        }
    }

}

